/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hcc.app;

import hcc.comp.FiFi_Csv;
import hcc.GUI.MainWindow;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.table.TableModel;

/**
 *
 * @author Guybrush
 */
public class Output {

    private CORE app;
    private MainWindow mw;
    private File csv;

    //Speichern...als csv
    public void saveCsv() throws IOException {
//        for (int it = 0; it < app.tables.size(); it++) {
            try {
                BufferedWriter bw = new BufferedWriter(new FileWriter(csv+".csv"));
                TableModel model = app.tables.get(0).getModel();
                for (int h = 0; h < model.getColumnCount(); h++) {
                    bw.write(model.getColumnName(h).toString());
                    if (h + 1 != model.getColumnCount()) {
                        bw.write(";");
                    }
                }
                bw.newLine();

                for (int clmCnt = model.getColumnCount(), rowCnt = model.getRowCount(), i = 0; i < rowCnt; i++) {
                    for (int j = 0; j < clmCnt; j++) {
                        if (model.getValueAt(i, j) != null) {
                            String value = model.getValueAt(i, j).toString();
                            bw.write(value);
                        }
                        if (j + 1 != clmCnt) {
                            bw.write(";");
                        }
                    }
                    bw.newLine();
                }

                bw.flush();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
//            }
        }
    }

    //Speichern unter....CSV
    public void saveToCsv() throws IOException {
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new FiFi_Csv());
        int returnVal = fc.showSaveDialog(mw);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            csv = fc.getSelectedFile();
            saveCsv();
        }
    }
}
